#include "g_renderer.h"

#include "g_shaders.h"

#include <iostream>
#include <conio.h>

using namespace std;

bool renderer::RUNNING = false;

void renderer::initOpenGL()
{
	cout<<"----- Initializing OpenGL -----\n\n";

	if (glfwInit() == GL_TRUE) cout<<"glfw ok.\n"; else 
	{
		cout<<"glfw init failed!\n";
			
		_getch();
		exit(0);
	}

	//turn on/off AA
	glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 8);
	
	//create Window
	glfwOpenWindow(WIDTH, HEIGHT, 0, 0, 0, 0, 0, 0, GLFW_WINDOW);
	glfwSetWindowTitle("Fly This!");

	//Set the frame buffer swap interval; 0 for vSyncing off and max framerate; 1 for Vsyncing on;
	glfwSwapInterval(0);

	if (glewInit() == GLEW_OK) cout<<"glew ok.\n"; else 
	{
		cout<<"glew init failed!\n";

		_getch();
		exit(0);
	}

	cout<<"OpenGL version: "<<glGetString(GL_VERSION)<<endl;
	cout<<"GLSL Version: "<<glGetString(GL_SHADING_LANGUAGE_VERSION)<<"\n\n";

	int aa_samples = glfwGetWindowParam(GLFW_FSAA_SAMPLES);
	
	if (aa_samples != 0) cout<<"Antialiasing: "<<aa_samples<<"x\n\n";
		else cout<<"Antialiasing disabled\n\n";
	
	cout<<"----- Initializing OpenGL -----\n\n";

	shaders::initShaders();

	setCamera();

	//ref_model = new Model("assets\\test_cube.obj");
	srand(unsigned int(glfwGetTime() * 100));

	RUNNING = true;
}

void renderer::setCamera()
{
	glViewport(0, 0, WIDTH, HEIGHT);

	glClearColor(0, 0, 0, 1);
	
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(70, WIDTH / (float) HEIGHT, 0.1, 700);
}

void renderer::cleanup()
{
	glfwTerminate();
}